﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SisVenda.Web.messageBox;
using SisVenda.Web.Code;

namespace SisVenda.Web.Restrito
{
    public partial class CadastrarProduto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MessageBox.Include(this);
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            gvwListagem.DataBind();     
        }

        protected void ddlNumeroLinhasGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvwListagem.DataBind();
        }

        protected void gvwListagem_DataBinding(object sender, EventArgs e)
        {
            gvwListagem.PageSize = Convert.ToInt16(ddlNumeroLinhasGrid.SelectedValue);
        }

        protected void gvwListagem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (fvwEdicao.CurrentMode != FormViewMode.ReadOnly)
            {
                fvwEdicao.ChangeMode(FormViewMode.ReadOnly);
            }
            fvwEdicao.DataBind();
        }

        protected void odsGridViewProduto_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (tbxDescricao.Text != "")
                e.InputParameters["descricao"] = tbxDescricao.Text;
        }

        protected void odsFormViewProduto_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            gvwListagem.DataBind();
        }

        protected void odsFormViewProduto_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            Label id_produto = (Label)fvwEdicao.FindControl("lblCodigoProduto");

            if (gvwListagem.SelectedDataKey != null)
                e.InputParameters["idProduto"] = gvwListagem.SelectedDataKey.Value;
            else
                e.InputParameters["idProduto"] = id_produto.Text;
            id_produto.Dispose();
        }

        protected void odsFormViewProduto_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            gvwListagem.DataBind();
        }

        protected void odsFormViewProduto_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (gvwListagem.SelectedDataKey != null)
                e.InputParameters["idProduto"] = gvwListagem.SelectedDataKey.Value; 

        }

        protected void odsFormViewProduto_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            gvwListagem.DataBind();
        }

        protected void odsFormViewProduto_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            TextBox cod = (TextBox)fvwEdicao.FindControl("tbxCodigoProdutoUp");
            TextBox desc = (TextBox)fvwEdicao.FindControl("tbxDescricaoProdutoUp");
            TextBox apel = (TextBox)fvwEdicao.FindControl("tbxApelidoUp");
            TextBox uni = (TextBox)fvwEdicao.FindControl("tbxUnidadeUp");
            TextBox pre = (TextBox)fvwEdicao.FindControl("tbxPrecoVendaUp");
            TextBox estMin = (TextBox)fvwEdicao.FindControl("tbxEstoqueMinimoUp");
            DropDownList cat = (DropDownList)fvwEdicao.FindControl("ddlCategoriaProdutoUp");
            DropDownList forn = (DropDownList)fvwEdicao.FindControl("ddlFornecedorUp");
            TextBox codFab = (TextBox)fvwEdicao.FindControl("tbxCodigoFabricanteUp");

            if (gvwListagem.SelectedDataKey != null)
                e.InputParameters["idProduto"] = gvwListagem.SelectedDataKey.Value;
            else
                e.InputParameters["idProduto"] = cod.Text;

            e.InputParameters["descricao"] = desc.Text;
            e.InputParameters["apelido"] = apel.Text;
            e.InputParameters["unidade"] = uni.Text;
            e.InputParameters["preco"] = pre.Text;
            e.InputParameters["estoqueMinimo"] = estMin.Text;
            e.InputParameters["categoria"] = cat.SelectedItem.Value;
            e.InputParameters["fornecedor"] = forn.SelectedItem.Value;
            e.InputParameters["codigoFabricante"] = codFab.Text;

            desc.Dispose();
            uni.Dispose();
            pre.Dispose();
            estMin.Dispose();
            cat.Dispose();
            forn.Dispose();
            cod.Dispose();
        }

        protected void odsFormViewProduto_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            TextBox est = (TextBox)fvwEdicao.FindControl("tbxEstoqueInsert");
            TextBox desc = (TextBox)fvwEdicao.FindControl("tbxDescricaoProdutoInsert");
            TextBox apel = (TextBox)fvwEdicao.FindControl("tbxApelidoInsert");
            TextBox uni = (TextBox)fvwEdicao.FindControl("tbxUnidadeInsert");
            TextBox pre = (TextBox)fvwEdicao.FindControl("tbxPrecoVendaInsert");
            TextBox estMin = (TextBox)fvwEdicao.FindControl("tbxEstoqueMinimoInsert");
            DropDownList cat = (DropDownList)fvwEdicao.FindControl("ddlCategoriaProdutoInsert");
            DropDownList forn = (DropDownList)fvwEdicao.FindControl("ddlFornecedorInsert");
            TextBox codFab = (TextBox)fvwEdicao.FindControl("tbxCodigoFabricanteInsert");

            e.InputParameters["estoque"] = est.Text;
            e.InputParameters["descricao"] = desc.Text;
            e.InputParameters["apelido"] = apel.Text;
            e.InputParameters["unidade"] = uni.Text;
            e.InputParameters["preco"] = pre.Text;
            e.InputParameters["estoqueMinimo"] = estMin.Text;
            e.InputParameters["categoria"] = cat.SelectedItem.Value;
            e.InputParameters["fornecedor"] = forn.SelectedItem.Value;
            e.InputParameters["codigoFabricante"] = codFab.Text;

            est.Dispose();
            desc.Dispose();
            uni.Dispose();
            pre.Dispose();
            estMin.Dispose();
            cat.Dispose();
            forn.Dispose();
            codFab.Dispose();
        }

        protected void fvwEdicao_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {

        }

        protected void ddlFornecedorInsert_SelectedIndexChanged(object sender, EventArgs e)
        {
            ObjectDataSource odsAuxiliar = (ObjectDataSource)fvwEdicao.FindControl("odsAuxInsert");
            DropDownList ddlAuxiliar = (DropDownList)fvwEdicao.FindControl("ddlAuxInsert");
            odsAuxiliar.DataBind();
            ddlAuxiliar.DataBind();
            TextBox cnpj = (TextBox)fvwEdicao.FindControl("tbxCnpjInsert");
            cnpj.Text = ddlAuxiliar.SelectedItem.ToString();

            odsAuxiliar.Dispose();
            ddlAuxiliar.Dispose();
            cnpj.Dispose();    
        }

        protected void ddlFornecedorUP_SelectedIndexChanged(object sender, EventArgs e)
        {
            ObjectDataSource odsAuxiliar = (ObjectDataSource)fvwEdicao.FindControl("odsAuxUpFor");
            DropDownList ddlAuxiliar = (DropDownList)fvwEdicao.FindControl("ddlAuxUpFor");
            odsAuxiliar.DataBind();
            ddlAuxiliar.DataBind();
            TextBox cnpj = (TextBox)fvwEdicao.FindControl("tbxCnpjUp");
            cnpj.Text = ddlAuxiliar.SelectedItem.ToString();
            
            odsAuxiliar.Dispose();
            ddlAuxiliar.Dispose();
            cnpj.Dispose();
        }


        protected void ddlCategoriaProdutoUp_SelectedIndexChanged(object sender, EventArgs e)
        {
            ObjectDataSource odsAuxiliar = (ObjectDataSource)fvwEdicao.FindControl("odsAuxUpCat");
            DropDownList ddlAuxiliar = (DropDownList)fvwEdicao.FindControl("ddlAuxUpCat");
            odsAuxiliar.DataBind();
            ddlAuxiliar.DataBind();
            TextBox cnpj = (TextBox)fvwEdicao.FindControl("tbxCnpjUp");
            cnpj.Text = ddlAuxiliar.SelectedItem.ToString();

            odsAuxiliar.Dispose();
            ddlAuxiliar.Dispose();
            cnpj.Dispose(); 
        }

        protected void ddlCategoriaProdutoInsert_SelectedIndexChanged(object sender, EventArgs e)
        {
            ObjectDataSource odsAuxiliar = (ObjectDataSource)fvwEdicao.FindControl("odsAuxCatInsert");
            DropDownList ddlAuxiliar = (DropDownList)fvwEdicao.FindControl("ddlAuxCatInsert");
            odsAuxiliar.DataBind();
            ddlAuxiliar.DataBind();
            TextBox cnpj = (TextBox)fvwEdicao.FindControl("tbxComissaoInsert");
            cnpj.Text = ddlAuxiliar.SelectedItem.ToString();

            odsAuxiliar.Dispose();
            ddlAuxiliar.Dispose();
            cnpj.Dispose(); 
        }

        protected void ddlCategoriaProdutoInsert_DataBound(object sender, EventArgs e)
        {
            TextBox com = (TextBox)fvwEdicao.FindControl("tbxComissaoInsert");
            ObjectDataSource odsCat = (ObjectDataSource)fvwEdicao.FindControl("odsAuxCatInsert");
            DropDownList ddlAuxCat = (DropDownList)fvwEdicao.FindControl("ddlAuxCatInsert");

            odsCat.DataBind();
            ddlAuxCat.DataBind();

            com.Text = ddlAuxCat.SelectedValue;

            com.Dispose();
            odsCat.Dispose();
            ddlAuxCat.Dispose();
        }

        protected void ddlFornecedorInsert_DataBound(object sender, EventArgs e)
        {
            TextBox cnpj = (TextBox)fvwEdicao.FindControl("tbxCnpjInsert");
            ObjectDataSource odsAuxFor = (ObjectDataSource)fvwEdicao.FindControl("odsAuxInsert");
            DropDownList ddlAuxFor = (DropDownList)fvwEdicao.FindControl("ddlAuxInsert");

            odsAuxFor.DataBind();
            ddlAuxFor.DataBind();

            cnpj.Text = ddlAuxFor.SelectedValue;

            cnpj.Dispose();
            ddlAuxFor.Dispose();
            odsAuxFor.Dispose();
        }
    }
}